/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "DXChatBarMoreView.h"

#define CHAT_BUTTON_SIZE 50
#define INSETS 8

#define MoveViewButtonPadding 10
#define MoveViewButtonHeight 60
#define MoveViewButtonWeight 60

@interface DXChatBarMoreView ()

@property (nonatomic, strong) UIButton *photoButton;
@property (nonatomic, strong) UIButton *takePicButton;
@property (nonatomic, strong) UIButton *locationButton;
@property (nonatomic, strong) UIButton *audioCallButton;
@property (nonatomic, strong) UIButton *videoCallButton;
@property (nonatomic, assign) ChatMoreType type;
@end

@implementation DXChatBarMoreView

#pragma mark - 懒加载
- (UIButton *)photoButton {
    if (_photoButton == nil) {
        _photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _photoButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_photoButton setImage:[UIImage imageNamed:@"chatBar_colorMore_photo"] forState:UIControlStateNormal];
        [_photoButton setImage:[UIImage imageNamed:@"chatBar_colorMore_photoSelected"] forState:UIControlStateHighlighted];
        [_photoButton addTarget:self action:@selector(photoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _photoButton;
}

- (UIButton *)takePicButton {
    if (_takePicButton == nil) {
        _takePicButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _takePicButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_takePicButton setImage:[UIImage imageNamed:@"chatBar_colorMore_camera"] forState:UIControlStateNormal];
        [_takePicButton setImage:[UIImage imageNamed:@"chatBar_colorMore_cameraSelected"] forState:UIControlStateHighlighted];
        [_takePicButton addTarget:self action:@selector(takePicAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _takePicButton;
}

- (UIButton *)locationButton {
    if (_locationButton == nil) {
        _locationButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _locationButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_locationButton setImage:[UIImage imageNamed:@"chatBar_colorMore_location"] forState:UIControlStateNormal];
        [_locationButton setImage:[UIImage imageNamed:@"chatBar_colorMore_locationSelected"] forState:UIControlStateHighlighted];
        [_locationButton addTarget:self action:@selector(locationAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _locationButton;
}

- (UIButton *)videoCallButton {
    if (_videoCallButton == nil) {
        _videoCallButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _videoCallButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_videoCallButton setImage:[UIImage imageNamed:@"chatBar_colorMore_videoCall"] forState:UIControlStateNormal];
        [_videoCallButton setImage:[UIImage imageNamed:@"chatBar_colorMore_videoCallSelected"] forState:UIControlStateHighlighted];
        [_videoCallButton addTarget:self action:@selector(takeVideoCallAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _videoCallButton;
}

- (UIButton *)audioCallButton {
    if (_audioCallButton == nil) {
        _audioCallButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _audioCallButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_audioCallButton setImage:[UIImage imageNamed:@"chatBar_colorMore_audioCall"] forState:UIControlStateNormal];
        [_audioCallButton setImage:[UIImage imageNamed:@"chatBar_colorMore_audioCallSelected"] forState:UIControlStateHighlighted];
        [_audioCallButton addTarget:self action:@selector(takeAudioCallAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _audioCallButton;
}

- (instancetype)initWithFrame:(CGRect)frame type:(ChatMoreType)type {
    self = [super initWithFrame:frame];
    if (self) {
        self.type = type;
        [self setupSubviewsForType:type];
        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)setupSubviewsForType:(ChatMoreType)type {

    // 更多界面
    // 1.好友：图片，拍照，位置，实时语音，实时视频
    // 2.群  ：图片，拍照，位置
    [self addSubview:self.photoButton];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.photoButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:0.2 constant:-(0.8 * MoveViewButtonWeight)]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.photoButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:MoveViewButtonPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.photoButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonHeight]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.photoButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonWeight]];
    
    [self addSubview:self.locationButton];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:0.4 constant:-(0.6 * MoveViewButtonWeight)]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:MoveViewButtonPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonHeight]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.locationButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonWeight]];
    
    [self addSubview:self.takePicButton];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.takePicButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:0.6 constant:-(0.4 * MoveViewButtonWeight)]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.takePicButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:MoveViewButtonPadding]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.takePicButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonHeight]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.takePicButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonWeight]];
    
    if (type == ChatMoreTypeChat) {
        [self addSubview:self.audioCallButton];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.audioCallButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:0.8 constant:-(0.2 * MoveViewButtonWeight)]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.audioCallButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:MoveViewButtonPadding]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.audioCallButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonHeight]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.audioCallButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonWeight]];
        
        [self addSubview:self.videoCallButton];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.videoCallButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:0.2 constant:-(0.8 * MoveViewButtonWeight)]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.videoCallButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:(MoveViewButtonPadding * 2 + MoveViewButtonHeight)]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.videoCallButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonHeight]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.videoCallButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:MoveViewButtonWeight]];
    }
}

#pragma mark - action

- (void)takePicAction {
    if([self.delegate respondsToSelector:@selector(moreViewTakePicAction:)]){
        [self.delegate moreViewTakePicAction:self];
    }
}

- (void)photoAction {
    if ([self.delegate respondsToSelector:@selector(moreViewPhotoAction:)]) {
        [self.delegate moreViewPhotoAction:self];
    }
}

- (void)locationAction {
    if ([self.delegate respondsToSelector:@selector(moreViewLocationAction:)]) {
        [self.delegate moreViewLocationAction:self];
    }
}

- (void)takeAudioCallAction {
    if ([self.delegate respondsToSelector:@selector(moreViewAudioCallAction:)]) {
        [self.delegate moreViewAudioCallAction:self];
    }
}

- (void)takeVideoCallAction {
    if ([self.delegate respondsToSelector:@selector(moreViewVideoCallAction:)]) {
        [self.delegate moreViewVideoCallAction:self];
    }
}

#pragma mark - public
- (CGFloat)frameHeight {
    if (self.type == ChatMoreTypeChat) {
        return MoveViewButtonPadding * 3 + MoveViewButtonHeight * 2;
    } else {
        return MoveViewButtonPadding * 2 + MoveViewButtonHeight;
    }
}
@end
